// pages/indexA/indexA.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    topTab: 0,
    tabA: 0,
    tabB: 0,
    arr: [
      { id: 2402816305,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 1569337684,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 3986918006,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 1734024538,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 9065570446,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 1368902479,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 4304394463,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 8521406130,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 3743898708,title: 'AirPods Pro',price: 1888,num: 1},
      { id: 7749399312,title: 'AirPods Pro',price: 1888,num: 1},
    ]
  },

  clickTab(e) {
    if ("toptab" === e.currentTarget.dataset.flag) {
      this.animationTopTab(e.currentTarget.dataset.toptab);
      this.setData({
        topTab: e.currentTarget.dataset.toptab
      })
    }
    if ("tabA" === e.currentTarget.dataset.flag) {
      this.animationTab(e.currentTarget.dataset.taba, 'tabaAnimation');
      this.setData({
        tabA: e.currentTarget.dataset.tab
      })
    }
    if ("tabB" === e.currentTarget.dataset.flag) {
      this.animationTab(e.currentTarget.dataset.taba, 'tabbAnimation');
      this.setData({
        tabB: e.currentTarget.dataset.tab
      })
    }
  },

  slideTab(e) {
    if ("toptab" === e.currentTarget.dataset.flag) {
      this.animationTopTab(e.detail.current);
      this.setData({
        topTab: e.detail.current
      })
    }
    if ("tabA" === e.currentTarget.dataset.flag) {
      this.animationTab(e.detail.current, 'tabaAnimation');
      this.setData({
        tabA: e.detail.current
      })
    }
    if ("tabB" === e.currentTarget.dataset.flag) {
      this.animationTab(e.detail.current, 'tabbAnimation');
      this.setData({
        tabB: e.detail.current
      })
    }
  },

  animationTopTab(topTab) {
    let animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    if (topTab === 0) {
      animation.left('-10%').step();
    } else {
      animation.left('50%').step();
    }
    this.setData({
      topbgAnimation: animation.export(),
    })
  },

  animationTab(tab, flag) {
    let animation = wx.createAnimation({
      duration: 500,
      timingFunction: 'ease',
    })
    if (tab === 0) {
      animation.left('42rpx').step();
    }
    if (tab === 1) {
      animation.left('230rpx').step();
    }
    if (tab === 2) {
      animation.left('418rpx').step();
    }
    if (tab === 3) {
      animation.left('606rpx').step();
    }
    this.setData({
      [flag]: animation.export(),
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.animationTopTab(0);

    let windowHeight = wx.getSystemInfoSync().windowHeight // 屏幕的高度
    let windowWidth = wx.getSystemInfoSync().windowWidth // 屏幕的宽度
    this.setData({
      scroll_height: (windowHeight * 750 / windowWidth) - 162,
      swiper_height: (windowHeight * 750 / windowWidth) - 100
    })

    let query = wx.createSelectorQuery();
    query.select('#top-tap').boundingClientRect(ress => {
      console.log('top-tap',ress)
      wx.getSystemInfo({
        success: res => {
          this.setData({
            topheight: (res.windowHeight - ress.top)
          })
        },
      })
    }).exec()
    query.select('#mid-tap').boundingClientRect(ress => {
      console.log('mid-tap', ress)
      wx.getSystemInfo({
        success: res => {
          this.setData({
            midheight: (res.windowHeight - ress.top)
          })
        },
      })
    }).exec()
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})